/**
 * @projectName interview_-assistant
 * @package org.example.util
 * @className org.example.util.ContentParser
 * @copyright Copyright 2025 Thunisoft, Inc All rights reserved.
 */
package org.example.util;

import org.example.pojo.entity.Article;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * ContentParser
 * @description
 * @author admin
 * @date 2025/4/14 10:38
 * @version TODO
 */
@Component
public class ContentParser {

    public List<Article> parseArticleList(String html) {
        // 解析列表页逻辑
        List<Article> articles = new ArrayList<>();
        Document doc = Jsoup.parse(html);
        Elements links = doc.select("a.tw-cursor-pointer");

        for (Element link : links) {
            Article article = new Article();
            article.setTitle(link.text().trim());
            String href = link.attr("href");
            article.setUrl(href.startsWith("/") ? "https://www.nowcoder.com" + href : href);
            articles.add(article);
        }
        return articles;
    }

    public void fillArticleDetail(String html, Article article) {
        // 解析详情页逻辑
        Document doc = Jsoup.parse(html);
        Element content = doc.selectFirst("div.feed-content-text, div.nc-post-content");
        Element time = doc.selectFirst("span.time-text");

        article.setContent(content != null ? content.text() : "");
        article.setPublishTime(time != null ? time.text() : "");
    }
}
